//
//  XYVisitorView.swift
//  SinaWeibo
//
//  Created by 韩啸宇 on 15/12/23.
//  Copyright © 2015年 backy. All rights reserved.
//

import UIKit

class XYVisitorView: UIView {

    /// 转盘
    @IBOutlet weak var rotationImageView: UIImageView!
    /// 显示图标
    @IBOutlet weak var iconImageView: UIImageView!
    /// 标题
    @IBOutlet weak var titleLabel: UILabel!
    /// 登录按钮
    @IBOutlet weak var loginButton: UIButton!
    /// 注册按钮
    @IBOutlet weak var registerButton: UIButton!
    
    /// 快速创建方法
    class func visitorView() -> XYVisitorView {
        return NSBundle.mainBundle().loadNibNamed("VisitorView", owner: nil, options: nil).last as! XYVisitorView
    }
    
    /// 快速设置方法: 显示图标的图片和标题
    func setVisitorInfo(imageName: String?, title: String) {

        // 1.设置标题
        titleLabel.text = title
        
        // 2.当前有没有传入图片(首页不需要传入)
        if let name = imageName {
            iconImageView.image = UIImage(named: name)
            rotationImageView.hidden = true
            
            return
        }
        
        // 3.旋转转盘
        startAnimation()
    }
    
    private func startAnimation() {
        // 1.创建动画对象
        let anim = CABasicAnimation(keyPath: "transform.rotation")

        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 10.0
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false // 告诉系统不要移除动画
        
        // 3.将动画添加到图层
        rotationImageView.layer.addAnimation(anim, forKey: nil)
    }
    
}
